# utf-8

"""
http://chromedriver.storage.googleapis.com/index.html
https://googlechromelabs.github.io/chrome-for-testing/#canary
"""

import time


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def create_browser():
    opts = webdriver.ChromeOptions()
    opts.add_argument('--headless')
    opts.add_argument('--start-maximized')
    driver_path = "/Users/aaron/Downloads/chromedriver-mac-x64/chromedriver"
    return webdriver.Chrome(service=Service(f"{driver_path}"), options=opts)


def handle_url(_url:str) -> str:
    split_over = _url.split("?envType")
    if len(split_over) != 2:
        return _url

    return split_over[0] + "description/"
    
def handle_title(_url:str) -> str:
    split_over = _url.split(" - ")
    if len(split_over) != 2:
        return _url

    return split_over[0]

def go_get():
    browser = create_browser()
    browser.get("https://leetcode.cn/studyplan/top-interview-150/")

    time.sleep(1)
    browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
 
    time.sleep(1)


    page_src = browser.page_source

    titles = browser.find_elements(By.XPATH, '//*[@class="truncate"]')

    for idx, title in enumerate(titles):
        title.click()
        WebDriverWait(browser, 10).until(EC.number_of_windows_to_be(2))

        new_win = browser.switch_to.window(browser.window_handles[-1])

        new_url = browser.current_url

        new_titil = browser.title

        browser.close()
        
        new_win = browser.switch_to.window(browser.window_handles[-1])

        new_url = handle_url(new_url)
        new_titil = handle_title(new_titil)

        print(idx, new_url)

        with open("./top150v2.csv", encoding="utf-8", mode="a+") as f:
            f.write(f'"{idx}","{new_titil}","{title.text}", "{new_url}"\n')

    browser.quit()

if __name__ == "__main__":
    go_get()

